package daizuo;

import daizuo.bean.Goods;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author cyt
 * @create 2020-07-21 19:16
 */
public class GoodMenu {

    public static void GoodList() throws IOException {
        Goods good1 = new Goods();
        good1.setId("1");
        good1.setGoodsName("addidas运动鞋");
        good1.setGoodsPrice(100);
        Goods good2 = new Goods();
        good2.setId("2");
        good2.setGoodsPrice(200);
        good2.setGoodsName("Kappa网球裙");
        Goods good3 = new Goods();
        good3.setId("3");
        good3.setGoodsPrice(300);
        good3.setGoodsName("网球拍");
        Goods good4 = new Goods();
        good4.setId("4");
        good4.setGoodsPrice(320);
        good4.setGoodsName("addidasT恤");

        System.out.println("------------------");
        System.out.println("选择商品编号");
        ArrayList<Goods> list = new ArrayList<>();
        list.add(good1);
        list.add(good2);
        list.add(good3);
        list.add(good4);

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "->  " + list.get(i).getGoodsName() + " - > " + list.get(i).getGoodsPrice());
        }


        System.out.println("------------------");

        GouMai(list);
    }

    public static void GouMai(ArrayList<Goods> list) throws IOException {
        ArrayList<Goods> arrayList = new ArrayList<>();
        Goods goods = XuanZe(list);
        arrayList.add(goods);
        System.out.println("是否继续（y / n ）");
        String isContinne = new Scanner(System.in).nextLine();
        while (isContinne.equals("y")) {
            Goods continueGood = XuanZe(list);
            arrayList.add(continueGood);
            isContinne = new Scanner(System.in).nextLine();
        }
        JieSuan(arrayList);
    }

    public static void JieSuan(ArrayList<Goods> list) throws IOException {
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).getId().equals(list.get(j).getId())) {
                    int num = list.get(i).getNum();
                    list.get(i).setNum(num + 1);
                    list.remove(j);
                }
            }
        }
        int sumPrice = 0;
        System.out.println("------------------");
        System.out.println("物品" + "      " + "单价" + "     " + "个数" + "    " + "金额");
        for (Goods goods : list) {
            System.out.println(goods.getGoodsName() + "  " + goods.getGoodsPrice() + "  " + goods.getNum() + "  " + (goods.getNum() * goods.getGoodsPrice()));
            sumPrice += goods.getNum() * goods.getGoodsPrice();
        }
        System.out.println("总计： " + sumPrice);
        System.out.println("输入支付金额： ");
        int nextInt = new Scanner(System.in).nextInt();
        System.out.println("找钱： " + String.valueOf(nextInt - sumPrice));
        System.out.println("获得积分- >  " + nextInt * 0.2);

        System.out.println("输入 n  返回上一级");
        String isReturn = new Scanner(System.in).nextLine();
        if (isReturn.equals("n")) {
            Menu.mainMenu();
        } else {
            GoodList();
        }
    }

    public static Goods XuanZe(ArrayList<Goods> list) {
        System.out.println("请输入商品编号");
        String pid = new Scanner(System.in).nextLine();

        System.out.println("请输入数目");
        int sum = new Scanner(System.in).nextInt();

        Goods ans = new Goods();
        for (Goods goods : list) {
            if (goods.getId().equals(pid)) {
                ans.setGoodsPrice(goods.getGoodsPrice());
                ans.setGoodsName(goods.getGoodsName());
                ans.setId(pid);
                ans.setNum(sum);
            }
        }
        return ans;
    }


    public static void main(String[] args) {

    }
}
